package cn.edu.hnnu.c.rentacar.dao.hibernateimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.edu.hnnu.c.rentacar.beans.RentRecord;
import cn.edu.hnnu.c.rentacar.idao.IRentRecordDAO;
import cn.edu.hnnu.c.rentacar.util.HibernateUtil;

public class RentRecordDAOHibernateImpl implements IRentRecordDAO {
	private static RentRecordDAOHibernateImpl instance;

	private RentRecordDAOHibernateImpl() {

	}

	public static RentRecordDAOHibernateImpl getInstace() {
		if (null == instance) {
			instance = new RentRecordDAOHibernateImpl();
		}
		return instance;
	}

	@Override
	public boolean addRentRecord(RentRecord rentRecord) {
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		
		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			session.save(rentRecord);
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		
		return true;

	}

	@Override
	public boolean deleteRentRecordById(int rentRecordId) {
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		RentRecord record = new RentRecord();
		record.setRentRecordId(rentRecordId);
		
		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			session.delete(record);
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		
		return true;

	}

	@Override
	public RentRecord getRentRecordById(int rentRecordId) {
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		RentRecord record = null;
		
		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			record = (RentRecord)session.load(RentRecord.class, rentRecordId);
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return null;
		}finally{
			session.close();
		}
		
		return record;
	}

	@Override
	public List<RentRecord> getAllRentRecords() {
		List<RentRecord> records = null;
		
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		
		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			records = session.createQuery("from RentRecord").list();
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return null;
		}finally{
			session.close();
		}
		
		return records;
		
	}

	@Override
	public boolean updateRentRecord(RentRecord rentRecord) {
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		
		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			session.update(rentRecord);
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		
		return true;
	}
}
